// pages/vipBuying/vipBuying.js
var util=require('../../utils/util.js');
const db=wx.cloud.database({});
Page({

  /**
   * 页面的初始数据
   */
  data: {
    card:[
      {
        "icon":"cloud://keepfit-cloud.6b65-keepfit-cloud-1302343813/VIP/vip_gold.png",
        "name":'金卡',
        "price":100,
        "des":'可购买课程、一周可五次进出健身房'
      },
      {
        "icon":"cloud://keepfit-cloud.6b65-keepfit-cloud-1302343813/VIP/vip_black.png",
        "name":'黑卡',
        "price":150,
        "des":'可购买课程、一周可十次进出健身房'
      },
      {
        "icon":"cloud://keepfit-cloud.6b65-keepfit-cloud-1302343813/VIP/vip_red.png",
        "name":'红卡',
        "price":200,
        "des":'可购买课程、一周可无限次进出健身房'
      }
    ],
    openid:'',
    isIdenti:false,
    cnt:1,
    event:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    that.setData({
      openid:options.openid
    })
    //获取时间
    var TIME=util.formatTime(new Date());
    this.setData({
      time:TIME,
    })
    db.collection('User').where({
      openid:this.data.openid
    }).get({
      success:res=>{
        console.log(res)
        this.setData({
          isIdenti:res.data[0].isIdenti
        })
        if(!this.data.isIdenti){
          this.setData({
            cnt:0
          })
        }
      }
    })
  },

  handleBuyClick(event){
    
    //使用promise处理异步问题
    // var that=this

    // that.setData({
    //   event:event
    // })
    // new Promise(function(resolve,reject){
    //   that.isIdentify(resolve);
    // }).then(function(){
    //   that.buyingVIP();
    // })
    var that=this
     if(that.data.cnt==0){
      wx.showModal({
        cancelColor: 'red',
        title:'请先实名认证',
        content:'购买会员要先进行实名认证',
        success:res=>{
          console.log(res)
          
          if(res.cancel){
            console.log('点击了取消')
          }

          if(res.confirm){
            console.log('点击了确定')
          }
        }
        
      })
    }
    console.log(event)


    if(that.data.cnt){
      wx.showModal({
        cancelColor: 'red',
        title:'购买'+event.currentTarget.dataset.item.name,
        content:'购买一个月会员，价格为'+event.currentTarget.dataset.item.price,
        success:res=>{
          console.log(res)
  
          if(res.cancel){
            console.log('点击了取消')
          }
          //在这里跳转到会员购买的页面
          if(res.confirm){
            db.collection('Record').add({
              data:{
                _openid:this.data.openid,
                studentID:this.data.openid,
                signTime:[]
              },
              success:function(res){
                console.log(res)
                  
              }
            })
            db.collection('VIPSales').add({
              data:{
                _openid:this.data.openid,
                buyingTime:this.data.time,
                VIPtype:event.currentTarget.dataset.item.name
              },
              success:function(res){
                console.log(res)
                  wx.showToast({
                    title: '购买成功',
                  })
              }
            })
          }
        }
      })
    }
    
  },
  isIdentify:function(resolve){
    var that=this
    if(!that.data.isIdenti){
      wx.showModal({
        cancelColor: 'red',
        title:'请先实名认证',
        content:'购买会员要先进行实名认证',
        success:res=>{
          console.log(res)
          that.setData({
            cnt:0
          })
          if(res.cancel){
            console.log('点击了取消')
          }

          if(res.confirm){
            console.log('点击了确定')
          }
        }
        
      })
    }
  },
  buyingVIP:function(){
    var that=this
    if(that.data.cnt){
      wx.showModal({
        cancelColor: 'red',
        title:'购买'+that.data.event.currentTarget.dataset.item.name,
        content:'购买一个月会员，价格为'+that.data.event.currentTarget.dataset.item.price,
        success:res=>{
          console.log(res)
  
          if(res.cancel){
            console.log('点击了取消')
          }
          //在这里跳转到会员购买的页面
          if(res.confirm){
            db.collection('Record').add({
              data:{
                _openid:this.data.openid,
                studentID:this.data.openid,
                signTime:[]
              },
              success:function(res){
                console.log(res)
                  
              }
            })
            db.collection('VIPSales').add({
              data:{
                _openid:this.data.openid,
                buyingTime:this.data.time,
                VIPtype:event.currentTarget.dataset.item.name
              },
              success:function(res){
                console.log(res)
                  wx.showToast({
                    title: '购买成功',
                  })
              }
            })
          }
        }
      })
    }
  }
})